package com.eqdd.yiqidian.ui.companyspace

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.loading.INetLoadingView
import com.eqdd.yiqidian.bean.OrganizationBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimImageSelectBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonCallBack
import com.eqdd.yiqidian.kotlin.BaseInfoActivity
import com.eqdd.yiqidian.utils.ImageUtil
import com.eqdd.yiqidian.utils.PicUtil
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_recyclerview.*
import kotlinx.android.synthetic.main.list_item_23.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast


/**
 * Created by jhj on 18-6-23.
 */
class OrganizationInfoActivity : BaseInfoActivity() {

    var dialog: INetLoadingView? = null

    override val topbar: String
        get() = "组织机构"

    override fun itemClick(p: Int) {

    }

    private var bean: OrganizationBean? = null

    override fun setEditBeans() {
        bean = intent.getSerializableExtra("data") as OrganizationBean?
        datas.add(SlimImageSelectBean("图片", 0, bean?.imageUrl).setDeal(true).setNeed(true))
        datas.add(SlimEditEnterBean("描述", bean?.describe, 1, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("排序", bean?.sort?.toString() ?: "", 2, "请输入").setNeed(true))
        slimAdapterEx.register(R.layout.layout_image_selected, SlimInjector<SlimImageSelectBean> { data, injector ->
            injector?.text(R.id.tv_label, data?.title)
                    ?.image(R.id.iv_add, data?.image)
                    ?.clicked(R.id.iv_add) {
                        PicUtil.single(this@OrganizationInfoActivity, true)
                    }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            val localMedias = PictureSelector.obtainMultipleResult(data)
            ImageUtil.setCircleImage(localMedias?.get(0)?.compressPath, iv_add)
            slimAdapterEx.getDataItem(0).tag = localMedias?.get(0)?.compressPath

        }
    }

    override fun setView() {
        super.setView()
        btn_submit.text = "修改"
        btn_submit.visibility = View.VISIBLE
        btn_submit.onClick {
            val data = slimAdapterEx.data
            OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.TORGANIZATION_UPDATE)
                    .params("userGuid", user.guid)
                    .params("companyId", user.companyId)
                    .params("imageId", bean?.id ?: 0)
                    .params("describe", data[1].content)
                    .params("sort", data[2].content)
                    .execute(object : JsonCallBack<HttpResult<*>>() {
                        override fun onSuccess(response: Response<HttpResult<*>>?) {
                            val result = response?.body()
                            if (result?.status == 200) {
                                toast("修改成功")
                                setResult(Activity.RESULT_OK)
                                finish()
                            }
                        }


                    })
        }

        initTopRightText("删除") {
            AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("是否删除成员${"“" + bean?.describe + "”?"}")
                    .setPositiveButton("取消", null)
                    .setNegativeButton("确定", DialogInterface.OnClickListener { dialogInterface, i ->
                        OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.ORGANIZATION_DELETE)
                                .params("userGuid", user.guid)
                                .params("companyId", user.companyId)
                                .params("imageId", bean?.id ?: 0)
                                .execute(object : JsonCallBack<HttpResult<*>>() {
                                    override fun onSuccess(response: Response<HttpResult<*>>?) {
                                        val result = response?.body()
                                        if (result?.status == 200) {
                                            toast("删除成功")
                                            setResult(Activity.RESULT_OK)
                                            finish()
                                        }
                                    }
                                })
                    })
                    .show()

        }

    }
}